package com.android.exchangeapp

import android.app.Activity
import android.content.res.Resources
import android.graphics.Color
import android.util.TypedValue
import androidx.core.view.WindowInsetsControllerCompat
import com.android.exchangeapp.modle.JumpInfo
import java.math.BigDecimal
import java.text.DecimalFormat

val decimalFormat: DecimalFormat by lazy { DecimalFormat(",###") }
val Int.dp
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this.toFloat(),
        Resources.getSystem().displayMetrics
    )

fun String.formatAmount(): String {
    if (isEmpty()) {
        return ""
    }
    return decimalFormat.format(toDouble())
}

fun Double.removeEndZeros(): String {
    val bigDecimal = BigDecimal(toString())
    return bigDecimal.stripTrailingZeros().toPlainString()
}

fun String?.toColor(): Int {
    if (isNullOrEmpty()) {
        return Color.TRANSPARENT
    }
    return try {
        Color.parseColor(this)
    } catch (e: Exception) {
        Color.TRANSPARENT
    }
}

fun Activity.applyStatusBar(jumpInfo: JumpInfo)  {
    val windowController = WindowInsetsControllerCompat(window, window.decorView)
    windowController.isAppearanceLightStatusBars = jumpInfo.statusBarStyle == 1
    window.statusBarColor = jumpInfo.statusBarColor.toColor()
}